package com.project.fiveCerealsSavoury.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.project.fiveCerealsSavoury.dao.AddressDao;
import com.project.fiveCerealsSavoury.domain.Address;
import com.project.fiveCerealsSavoury.exception.BizException;
import com.project.fiveCerealsSavoury.service.AddressService;
import com.project.fiveCerealsSavoury.transfer.AddressSet;

@Service
public class AddressServiceImpl implements AddressService {
	@Autowired
	private AddressDao addressDao;
	
	@Override
	public boolean add(Address address) throws BizException {
		if (address.getAddress() == null || address.getAddress().trim().length()<0) throw new BizException("收货地址不能为空");
		if (address.getNickName() == null || address.getNickName().trim().length() ==0) throw new BizException("收货人昵称不能为空");
		if (address.getUserPhone() == null || address.getUserPhone().trim().length()==0) throw new BizException("收货人联系方式不能为空");
		if (address.getUserId() == null) throw new BizException("该用户不存在");
		if (address.getDetailedAddress() == null || address.getDetailedAddress().trim().length()==0) throw new BizException("详细地址不能为空");
		addressDao.add(address);
		return true;
	}
	
	@Override
	public List<Address> queryList(Integer userId) throws BizException{
		if (userId == null) throw new BizException("用户id不能为空");
		List<Address> address = addressDao.queryList(userId);
		return address;
	}

	@Override

	public Integer remove(Integer id) throws BizException {	
		Integer remove = addressDao.remove(id);
		return remove;
	}

	@Override
	public Address query(Integer id) throws BizException {
		Address query = addressDao.query(id);
		if(query !=null){
			return query;
		}
		return null;
	}


	@Override
	public void modify(AddressSet addressSet) throws BizException {
		if(addressSet.getNewAddress() == null) throw new BizException("新地址补鞥呢为空");
		if(addressSet.getNewNickName() == null)throw new BizException("收货人不能为空");
		if(addressSet.getNewUserPhone() == null)throw new BizException("收货电话不能为空");
		if(addressSet.getNewDetailedAddress() == null) throw new BizException("详细地址不能为空");
		addressDao.modify(addressSet);
	}

	@Override
	public Address queryAddress(Integer id) throws BizException {
		if (id == null) throw new BizException("id不能为空");
		return addressDao.queryAddress(id);
	}

}
